package pl.kosmowska.ewelina.client.views.impl;

import java.util.Date;
import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.OutcomeDataProxy;
import pl.kosmowska.ewelina.client.views.IncomeOutcomeView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;
import pl.kosmowska.ewelina.client.views.widgets.grids.OutcomeDataGrid;
import pl.kosmowska.ewelina.client.views.widgets.popup.PopupMessage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class IncomeOutcomeViewImpl extends Composite implements IncomeOutcomeView{

	private static OutcomeViewImplUiBinder uiBinder = GWT
			.create(OutcomeViewImplUiBinder.class);

	@UiField DockLayoutPanel outcomeLayout;
	@UiField ResizeLayoutPanel outcomePanel;

	@UiField Button loadOutcomes;
	@UiField Label bilansBox;
	@UiField Label outcomeBox;
	@UiField Label incomeBox;
	@UiField Panel startDatePanel;
	@UiField Panel endDatePanel;

	interface OutcomeViewImplUiBinder extends UiBinder<Widget, IncomeOutcomeViewImpl> {
	}

	private Presenter presenter;
	private boolean alreadyInit;


	private OutcomeDataGrid<OutcomeDataProxy> outcomeGrid = new OutcomeDataGrid<OutcomeDataProxy>();
	private DateBox startDateBox = new DateBox();
	private DateBox endDateBox = new DateBox();

	public IncomeOutcomeViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		startDatePanel.add(initDateBox(startDateBox, "Od: "));
		endDatePanel.add(initDateBox(endDateBox, "Do: "));

	}

	public HorizontalPanel initDateBox(DateBox dateBox, String name){
		DateTimeFormat dateFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.YEAR_MONTH_DAY);
		dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
		HorizontalPanel vPanel = new HorizontalPanel();
		vPanel.add(new HTML(name));
		vPanel.add(dateBox);
		return vPanel;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}


	@Override
	public void clearView() {
		bilansBox.setText("0.00");
		outcomeBox.setText("0.00");
		incomeBox.setText("0.00");
		outcomePanel.clear();

	}

	public void initPagingDataGrid(	List<OutcomeDataProxy> returnedOutcomeData){
		outcomeGrid.setHeight("800px");
		outcomeGrid.setWidth("100%");
		outcomeGrid.setDataList(returnedOutcomeData);	
	}

	@Override
	public void reInitView(float balanceSum, float incomeSum, float outcomeSum,
			List<OutcomeDataProxy> returnedOutcomeData) {
		//debug
		System.out.println("IncomeOutcomeViewImpl.setLoggedIn(): Loading IncomeOutcome");	
		bilansBox.setText(String.valueOf(balanceSum));
		outcomeBox.setText(String.valueOf(outcomeSum));
		incomeBox.setText(String.valueOf(incomeSum));
		outcomePanel.clear();
		initPagingDataGrid(returnedOutcomeData);
		outcomePanel.add(outcomeGrid);
	}

	@Override
	public void showMessage(String message) {
//		MessageDialogBox messageDialogBox = new MessageDialogBox(message);
//		messageDialogBox.center();
//		messageDialogBox.show();
		
        final PopupMessage alertMessage = new PopupMessage(message);
        alertMessage.startProcessing();
        Timer t = new Timer() {
            public void run() {
                alertMessage.stopProcessing();
            }
        };
        t.schedule(1000);
	}

	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}
	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}

	@UiHandler("loadOutcomes")
	void onLoadOutcomesClick(ClickEvent event) {
		Date startDate =startDateBox.getValue();
		Date endDate =endDateBox.getValue();
		if(startDate==null || endDate==null){
			showMessage(ViewMessages.ERROR_NO_DATES);
			return;
		}else if (startDate.getTime()> endDate.getTime()){
			showMessage(ViewMessages.ERROR_WRONG_DATES);
			return;
		}

		java.sql.Date sqlDate1 = new java.sql.Date(startDate.getTime());
		java.sql.Date sqlDate2 = new java.sql.Date(endDate.getTime());
		
		presenter.loadDataForIncomeOutcomeView(sqlDate1, sqlDate2);
	}


}


